package yuhf.servlet;

import com.mysql.cj.Session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet(urlPatterns ={"/login.Servlet"})
public class LoginServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String name=request.getParameter("name");
        String passwd=request.getParameter("passwd");
        String isCookie=request.getParameter("isCookie");
        if (isCookie!=null){
            Cookie cookie=new Cookie("firstCookie",name);
            cookie.setMaxAge(10);
            response.addCookie(cookie);
        }
        HttpSession session=request.getSession();
        session.setAttribute("name",name);

        if ("admin".equals(name)&&"admin".equals(passwd)){
            request.getRequestDispatcher("view/show.jsp").forward(request,response);
        }else {
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }
    }
}
